package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.mmall.util.CookieUitl;
import com.mmall.util.JasonUtil;
import com.mmall.util.RedisShardedPoolUtil;
import com.mmall.util.SessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by root on 3/18/18.
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;


    @ResponseBody
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    public ServerResponse login(HttpServletRequest request, HttpServletResponse response, String username, String password){
        ServerResponse resp = iUserService.login(username, password);
        if (resp.isSuccess()){
            HttpSession sesssion = request.getSession();
            String sessionId = sesssion.getId();
            CookieUitl.writeSessionIdCookie(response, sessionId);
            RedisShardedPoolUtil.set(sessionId, JasonUtil.object2Str(resp.getData()));
            RedisShardedPoolUtil.expired(sessionId, Const.RedisCacheExTime.REDIS_SESSION_EXTIME);
            //sesssion.setAttribute(Const.CURRENT_USER, resp.getData());
        }
        return resp;
    }

    @ResponseBody
    @RequestMapping(value = "logout.do", method = RequestMethod.GET)
    public ServerResponse logout(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        String sessionId = CookieUitl.readSessionId(request);
        if (!StringUtils.isBlank(sessionId)) {
            CookieUitl.delSessionIdCookie(request, response);
            RedisShardedPoolUtil.del(sessionId);
        }
        //session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    @ResponseBody
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.GET)
    public ServerResponse getUserInfo(HttpServletRequest request, HttpSession session){
        //User user = (User)session.getAttribute(Const.CURRENT_USER);
        User user = SessionUtil.getCurrentUser(request);
        if (user == null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(), "login first.");
        }
        return ServerResponse.createBySuccess(user);
    }

    @ResponseBody
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    public ServerResponse register(User user){
        return iUserService.register(user);
    }

    @ResponseBody
    @RequestMapping(value = "check_valid.do", method = RequestMethod.POST)
    public ServerResponse checkValid(String value, String type){
        return iUserService.validCheck(value, type);
    }

    @ResponseBody
    @RequestMapping(value = "forget_get_question.do", method = RequestMethod.POST)
    public ServerResponse forgetGetQuestion(String username){
        return iUserService.selectQuestion(username);
    }

    @ResponseBody
    @RequestMapping(value = "forget_ckeck_answer.do", method = RequestMethod.POST)
    public ServerResponse forgetCheckAnswer(String username, String question, String answer){
        return iUserService.forgetCheckAnswer(username, question, answer);
    }

    @ResponseBody
    @RequestMapping(value = "forget_reset_password.do", method = RequestMethod.POST)
    public ServerResponse forgetResetPassword(String username, String newPsw, String token){
        return iUserService.forgetCheckAnswer(username,newPsw,token);
    }

    @ResponseBody
    @RequestMapping(value = "reset_password.do" ,method = RequestMethod.POST)
    public  ServerResponse resetPassword(HttpSession session, String passwordOld, String passwordNew){
        User curUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (curUser == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iUserService.resetPassword(curUser, passwordOld, passwordNew);
    }

    @ResponseBody
    @RequestMapping(value = "update_information.do", method = RequestMethod.POST)
    public ServerResponse updateInformation(HttpSession session, User user){
        User curUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (curUser == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        user.setId(curUser.getId());
        user.setUsername(curUser.getUsername());
        return  iUserService.updateUserInfo(user);
    }

    @ResponseBody
    @RequestMapping(value = "get_information")
    public ServerResponse getInformation(HttpSession session){
        User curUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (curUser == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iUserService.getUserById(curUser.getId());
    }
}
